package com.inspur.dgov.bsp.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inspur.dgov.bsp.user.entity.PubOrgan;
import com.inspur.dgov.bsp.user.entity.PubOrganRelation;
import com.inspur.dgov.bsp.user.mapper.PubOrganMapper;
import com.inspur.dgov.bsp.user.service.PubOrganRelationService;
import com.inspur.dgov.bsp.user.service.PubOrganService;
import com.inspur.dgov.bsp.user.vo.OrganVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 组织信息 服务实现类
 *
 * @author chiyupei
 * @version 1.0
 * @since 1.0
 */
@Slf4j
@Service
@AllArgsConstructor
public class PubOrganServiceImpl extends ServiceImpl<PubOrganMapper, PubOrgan> implements PubOrganService {

    @Autowired
    private PubOrganRelationService pubOrganRelationService;

    /**
     * 组织机构分页数据
     * @param page
     * @param params
     * @return
     */
    @Override
    public IPage<OrganVO> selectOrganPage(Page page, Map<String, Object> params) {
        return baseMapper.selectOrganPage(page, params);
    }

    /**
     * 添加组织机构
     * @param organ
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrgan(PubOrgan organ) {
        boolean rs = super.save(organ);
        //添加组织机构关系
        Long id = organ.getId();
        Long parentId = organ.getParentId();
        //添加自身关系
        PubOrganRelation own = new PubOrganRelation();
        own.setAncestorId(id);
        own.setDescendantId(id);
        own.setDistance(0);
        this.pubOrganRelationService.save(own);
        //添加递归关系
        if (null != parentId) {
            PubOrganRelation relation = new PubOrganRelation();
            relation.setAncestorId(id);
            relation.setDescendantId(parentId);
            this.pubOrganRelationService.insertRecursionOrganRelation(relation);
        }
        return rs;
    }

    /**
     * 移动组织机构
     * @param organ
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void moveOrgan(PubOrgan organ) {
        PubOrganRelation relation = new PubOrganRelation();
        relation.setAncestorId(organ.getId());
        relation.setDescendantId(organ.getParentId());
        //分离子树
        this.pubOrganRelationService.separateOrganRelation(relation);
        //移动子树
        this.pubOrganRelationService.moveOrganRelation(relation);
    }

    /**
     * 删除组织机构
     * @param organ
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteOrgan(PubOrgan organ) {
        Long id = organ.getId();
        //删除组织机构关系
        UpdateWrapper<PubOrganRelation> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(PubOrganRelation::getAncestorId, id)
                .or().eq(PubOrganRelation::getDescendantId, id);
        this.pubOrganRelationService.remove(wrapper);
        return super.removeById(id);
    }
}
